import * as vscode from 'vscode';
import { TodoProvider } from '../providers/todo-provider';
import { StatsProvider } from '../providers/stats-provider';
import { TaskStatus, TaskTag } from '../models/todo';

export class CommandService {
    constructor(
        private context: vscode.ExtensionContext,
        private todoProvider: TodoProvider,
        private statsProvider: StatsProvider
    ) {}

    registerCommands(): void {
        this.context.subscriptions.push(
            vscode.commands.registerCommand('easytodo.createTask', this.createTask.bind(this)),
            vscode.commands.registerCommand('easytodo.createGroup', this.createGroup.bind(this)),
            vscode.commands.registerCommand('easytodo.deleteTask', this.deleteTask.bind(this)),
            vscode.commands.registerCommand('easytodo.deleteGroup', this.deleteGroup.bind(this)),
            vscode.commands.registerCommand('easytodo.editGroupName', this.editGroupName.bind(this)),
            vscode.commands.registerCommand('easytodo.updateTaskStatus', this.updateTaskStatus.bind(this)),
            vscode.commands.registerCommand('easytodo.updateTaskTag', this.updateTaskTag.bind(this))
        );
    }

    private async createTask(): Promise<void> {
        const title = await vscode.window.showInputBox({
            placeHolder: '输入任务标题',
            prompt: '创建新任务',
        });
        console.log('>>>> title >>>',title);
        if (title) {
            await this.todoProvider.createTask(title);
            this.statsProvider.refresh();
        }
    }

    private async createGroup(): Promise<void> {
        const name = await vscode.window.showInputBox({
            placeHolder: '输入分组名称',
            prompt: '创建新分组',
        });
        if (name) {
            await this.todoProvider.createGroup(name);
            this.statsProvider.refresh();
        }
    }

    private async deleteTask(item: any): Promise<void> {
        if (item?.task?.id) {
            const confirmed = await vscode.window.showWarningMessage(
                '确定要删除这个任务吗？',
                { modal: true },
                '确定',
                '取消'
            );
            if (confirmed === '确定') {
                await this.todoProvider.deleteTask(item.task.id);
                this.statsProvider.refresh();
            }
        }
    }

    private async deleteGroup(item: any): Promise<void> {
        if (item?.task?.id) {
            const confirmed = await vscode.window.showWarningMessage(
                '确定要删除这个分组吗？',
                { modal: true },
                '确定',
                '取消'
            );
            if (confirmed === '确定') {
                await this.todoProvider.deleteGroup(item.task.id);
                this.statsProvider.refresh();
            }
        }
    }

    private async editGroupName(item: any): Promise<void> {
        if (item?.task?.id) {
            const newName = await vscode.window.showInputBox({
                placeHolder: '输入新的分组名称',
                prompt: '编辑分组名称',
                value: item.task.name,
            });
            if (newName) {
                await this.todoProvider.updateGroupName(item.task.id, newName);
                this.statsProvider.refresh();
            }
        }
    }

    private async updateTaskStatus(item: any): Promise<void> {
        if (item?.task?.id) {
            const status = await vscode.window.showQuickPick(
                [
                    { label: '未开始', value: TaskStatus.Pending },
                    { label: '处理中', value: TaskStatus.InProgress },
                    { label: '已完成', value: TaskStatus.Done },
                    { label: '已取消', value: TaskStatus.Cancelled },
                ],
                {
                    placeHolder: '选择任务状态',
                }
            );
            if (status) {
                await this.todoProvider.updateTaskStatus(item.task.id, status.value);
                this.statsProvider.refresh();
            }
        }
    }

    private async updateTaskTag(item: any): Promise<void> {
        if (item?.task?.id) {
            const tag = await vscode.window.showQuickPick(
                [
                    {
                        label: '重要且紧急',
                        value: TaskTag.ImportantUrgent,
                        iconPath: new vscode.ThemeIcon(
                            'tag',
                            new vscode.ThemeColor('#e74c3c')
                        ),
                    },
                    {
                        label: '重要不紧急',
                        value: TaskTag.ImportantNotUrgent,
                        iconPath: new vscode.ThemeIcon(
                            'tag',
                            new vscode.ThemeColor('#f1c40f')
                        ),
                    },
                    {
                        label: '不重要但紧急',
                        value: TaskTag.NotImportantUrgent,
                        iconPath: new vscode.ThemeIcon(
                            'tag',
                            new vscode.ThemeColor('#3498db')
                        ),
                    },
                    {
                        label: '不重要不紧急',
                        value: TaskTag.NotImportantNotUrgent,
                        iconPath: new vscode.ThemeIcon(
                            'tag',
                            new vscode.ThemeColor('#95a5a6')
                        ),
                    },
                ],
                {
                    placeHolder: '选择任务标签',
                }
            );
            if (tag) {
                await this.todoProvider.updateTaskTag(item.task.id, tag.value);
                this.statsProvider.refresh();
            }
        }
    }
}
